package zone.czh.woi.protocol.util;

import com.google.protobuf.ByteString;
import zone.czh.woi.protocol.protocol.Payload;
import zone.czh.woi.protocol.protocol.WoiProtobuf;

/**
*@ClassName: ProtobufPayloadUtil
*@Description: None
*@author woi
*/
public class ProtobufPayloadUtil {

    public static WoiProtobuf.Payload buildPayload(Payload payload){
        return buildPayload(
                payload.getSn(),
                payload.getCmd(),
                payload.getType(),
                payload.isResponseRequired(),
                payload.getRespondSn(),
                payload.getDcn(),
                payload.getData());
    }

    public static WoiProtobuf.Payload buildPayload(long sn, String cmd,int type,boolean responseRequired,long respondSn,byte[] dcn,byte[] data){
        WoiProtobuf.Payload.Builder builder = WoiProtobuf.Payload.newBuilder();
        if (cmd==null){
            return null;
        }
        builder.setSn(sn);
        builder.setCmd(cmd);
        builder.setType(type);
        builder.setResponseRequired(responseRequired);
        builder.setRespondSn(respondSn);
        if (dcn!=null){
            builder.setDcn(ByteString.copyFrom(dcn));
        }
        if (data!=null){
            builder.setData(ByteString.copyFrom(data));
        }

        return builder.build();

    }

    public static Payload parse(WoiProtobuf.Payload payload){
        return new Payload().setSn(payload.getSn())
                .setCmd(payload.getCmd())
                .setType(payload.getType())
                .setResponseRequired(payload.getResponseRequired())
                .setRespondSn(payload.getRespondSn())
                .setDcn(payload.getDcn().toByteArray())
                .setData(payload.getData().toByteArray());
    }
}
